import { ref } from 'vue';
import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { list as deviceTypeList } from '../deviceType/deviceType.api';
import { getDeviceModelByType } from '../deviceModel/deviceModel.api';
import { h } from 'vue';
import { Icon } from '/@/components/Icon';
import { render } from '/@/utils/common/renderUtils';
import { isNullOrUnDef, isEmpty } from '/@/utils/is';



const type = [
  {label: '点位', value: 1},
  {label: '自定义', value: 2},
];

const accuracyOptions = [
  {label: '0位', value: 0},
  {label: '1位', value: 1},
  {label: '2位', value: 2},
  {label: '3位', value: 3},
  {label: '4位', value: 4},
  {label: '5位', value: 5},
];

// HEX, UTF8, ASCII
const encode = [
  {label: 'HEX', value: 'HEX'},
  {label: 'UTF8', value: 'UTF8'},
  {label: 'ASCII', value: 'ASCII'},
];

export const paramDeviceTypeId = ref('');

export const columns: BasicColumn[] = [
  {
    title: '点位名称',
    dataIndex: 'name',
    width: 200,
    align: 'left',
  },
  {
    title: '点位地址',
    dataIndex: 'address',
    width: 150,
    align: 'left',
  },
  {
    title: '所属型号',
    dataIndex: 'modelName',
    width: 120,
  },
  {
    title: '字段名称',
    dataIndex: 'fieldName',
    width: 150,
  },
  {
    title: '字段类型',
    dataIndex: 'fieldType',
    width: 150,
    customRender: ({ text }) => render.renderDict(text, 'iot_field_type'),
  },
  // {
  //   title: '精度',
  //   dataIndex: 'accuracy',
  //   width: 100,
  //   customRender: ({ text }) => {
  //     const list = accuracyOptions.filter((v)=>v.value == text)
  //     if(list.length>0){
  //       return list[0].label;
  //     }
  //     return text;
  //   }
  // },
  {
    title: '点位类型',
    dataIndex: 'type',
    width: 90,
    customRender: ({ text }) => {
      const list = type.filter((v)=>v.value == text)
      if(list.length>0){
        return list[0].label;
      }
      return text;
    }
  },
  {
    title: '报文编码',
    dataIndex: 'encode',
    width: 90,
  },
  {
    title: '自定义报文',
    dataIndex: 'message',
    width: 280,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '点位名称',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'fieldName',
    label: '字 段 名',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'modelId',
    label: '设备型号',
    component: 'ApiSelect',
    colProps: { span: 6 },
    componentProps: ({ formModel }) => {
      return {
        api: getDeviceModelByType,
        params: {typeId: paramDeviceTypeId.value},
        labelField: 'model',
        valueField: 'id',
        getPopupContainer: () => document.body,
      };
    },
  },
];

export const formSchema: FormSchema[] = [
  {
    label: 'id',
    field: 'id',
    component: 'Input',
    show: false,
  },
  // {
  //   label: '所属型号',
  //   field: 'modelId',
  //   component: 'ApiSelect',
  //   required: true,
  //   componentProps: ({ formModel }) => {
  //     return {
  //       api: getDeviceModelByType,
  //       params: {typeId: paramDeviceTypeId.value},
  //       labelField: 'model',
  //       valueField: 'id',
  //       getPopupContainer: () => document.body,
  //     };
  //   },
  //   helpMessage: '请先选择左测的设备类型'
  // },  
  {
    label: '所属型号',
    field: 'modelId',
    component: 'Select',
    required: true,
    helpMessage: '请先选择左测的设备类型'
  },  
  {
    label: '点位名称',
    field: 'name',
    component: 'Input',
    required: true,
  },
  {
    label: '字段名',
    field: 'fieldName',
    component: 'Input',
    required: true,
  },
  {
    label: '类型',
    field: 'type',
    component: 'RadioGroup',
    defaultValue: 1,
    componentProps: {
      //options里面由一个一个的radio组成,支持disabled禁用
      options: type,
    },
  },
  {
    label: '点位地址',
    field: 'address',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => values.type == 1,
  },
  {
    label: '字段类型',
    field: 'fieldType',
    component: 'JDictSelectTag',
    componentProps: ({ formActionType, formModel }) => {
      return {
        dictCode: 'iot_field_type',
        placeholder: '',
        stringToNumber: false,
        onSelect: (options, values) => {
          if(options == 5 || options == 6) {
            formModel.accuracy = 2;
          } else {
            formModel.accuracy = null;
          }
        },
      };
    },
    ifShow: ({ values }) => values.type == 1,
  },
  {
    label: '浮点精度',
    field: 'accuracy',
    component: 'Select',
    componentProps: {
      options: accuracyOptions,
      getPopupContainer: () => document.body,
    },
    required: true,
    show: false,
  },
  {
    label: '报文编码',
    field: 'encode',
    component: 'RadioGroup',
    required: true,
    componentProps: {
      //options里面由一个一个的radio组成,支持disabled禁用
      options: encode,
    },
    ifShow: ({ values }) => values.type == 2,
  },
  {
    label: '自定义报文',
    field: 'message',
    component: 'InputTextArea',
    required: true,
    ifShow: ({ values }) => values.type == 2,
  },
];